package com.sou.technologytool.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.benben.commoncore.utils.StatusBarUtils;
import com.facebook.appevents.AppEventsLogger;
import com.just.agentweb.AgentWeb;
import com.sou.technologytool.R;
import com.sou.technologytool.base.BaseActivity;


import butterknife.BindView;

public class PolicyActivity extends BaseActivity {
    @BindView(R.id.wb_view)
    WebView webView;
    private String level = "";
    private AgentWeb mAgentWeb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_policy;
    }

    @Override
    protected void initData() {
        StatusBarUtils.fullScreen(this,R.color.black);
        StatusBarUtils.setAndroidNativeLightStatusBar(mContext, needStatusBarDarkText());
        level = getIntent().getStringExtra("level");

        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(webView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))//
                .useDefaultIndicator(Color.parseColor("#FF2D52"), 2)//
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go(level);

        mAgentWeb.getAgentWebSettings().getWebSettings().setUserAgentString("dsbrowser_android");
        mAgentWeb.getAgentWebSettings().getWebSettings().setJavaScriptEnabled(true);
        mAgentWeb.getAgentWebSettings().getWebSettings().setBlockNetworkImage(false);
        mAgentWeb.getAgentWebSettings().getWebSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//
//        WebSettings setting = mAgentWeb.getAgentWebSettings().getWebSettings();
//        /**设置可以访问文件 **/
//        setting.setAllowFileAccess(true);
//        setting.setAllowFileAccessFromFileURLs(true);
//        setting.setAllowUniversalAccessFromFileURLs(true);

        mAgentWeb.getJsInterfaceHolder().addJavaObject("android", new AndroidInterface());
//        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//        webView.getSettings().setJavaScriptEnabled(true);
//        webView.getSettings().setSupportZoom(true);
//        webView.getSettings().setBuiltInZoomControls(true);
//        webView.getSettings().setUseWideViewPort(true);
//        webView.getSettings().setLoadWithOverviewMode(true);
////        webView.getSettings().setAppCacheEnabled(true);
//        webView.getSettings().setDomStorageEnabled(true);//DOM Storage
//        webView.loadUrl(level);
//        webView.addJavascriptInterface(PolicyActivity.this, "android");
//        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (url.startsWith("http://") || url.startsWith("https://")) {
//                    return false;
//                }
//                try {
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                    startActivity(intent);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return true;
//
//            }
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                StyledDialogUtils.getInstance().dismissLoading();
//            }
//
//            @Override
//            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                super.onPageStarted(view, url, favicon);
//                StyledDialogUtils.getInstance().loading(PolicyActivity.this);
//            }
//        });
//    }

//    protected WebChromeClient mWebChromeClient = new WebChromeClient() { }
//
    }

    public class AndroidInterface {

        @JavascriptInterface
        public void jsAndroid(final String text) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    AppEventsLogger logger = AppEventsLogger.newLogger(PolicyActivity.this);
                    Bundle bundle = new Bundle();
                    bundle.putString("fb_currency", "INR");
                    logger.logEvent("fb_mobile_purchase", rd(), bundle);
                }
            });
        }
    }

    private long firstTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mAgentWeb.handleKeyEvent(keyCode, event)) {
            return true;
        }
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                System.exit(0);
            } else {
                Toast.makeText(PolicyActivity.this, "Press again to exit the program", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }
    private int rd(){
        // 随机生成100到500之间的整数
        int min = 100;
        int max = 500;
        return (int)(Math.random() * (max - min + 1)) + min;
    }

}
